package com.keba.lamp.complaint.biz;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.keba.lamp.complaint.entity.AccountPrepayment;
import com.keba.lamp.complaint.service.AccountPrepaymentService;
import com.keba.lamp.complaint.vo.save.AccountPrepaymentSaveVO;
import com.keba.lamp.complaint.vo.update.AccountPrepaymentUpdateVO;
import com.keba.lamp.system.entity.tenant.DefTenant;
import com.keba.lamp.system.service.tenant.DefTenantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Objects;

@Component
@Slf4j
@RequiredArgsConstructor
public class AccountBiz {

    private final DefTenantService defTenantService;

    private final AccountPrepaymentService accountPrepaymentService;

    public void addAccount(Long tenantId, String tenantName) {
        DefTenant defTenant = defTenantService.getById(tenantId);
        accountPrepaymentService.save(new AccountPrepaymentSaveVO().setName(tenantName).setBalance(BigDecimal.ZERO).setTenantId(defTenant.getId()).setCode(DateUtil.format(DateUtil.date(), DatePattern.PURE_DATE_PATTERN) + RandomUtil.randomNumbers(4)));
    }

    public void updateAccount(Long tenantId, String tenantName) {
        DefTenant defTenant = defTenantService.getById(tenantId);
        AccountPrepayment accountPrepayment = accountPrepaymentService.getByTenantId(tenantId);
        if (Objects.isNull(accountPrepayment)) {
            accountPrepaymentService.save(new AccountPrepaymentSaveVO().setBalance(BigDecimal.ZERO).setTenantId(defTenant.getId()).setCode(DateUtil.format(DateUtil.date(), DatePattern.PURE_DATE_PATTERN) + RandomUtil.randomNumbers(4)));
            return;
        }
        // 更新账户信息
        accountPrepaymentService.updateById(new AccountPrepaymentUpdateVO().setName(tenantName).setId(accountPrepayment.getId()));
    }

}
